<?php 

class YouNet_Mail
{
	private $_mailer;	
	protected $_charset = 'UTF-8';
    
    
    protected $_from = 'media@younetco.com';
    protected $_from_name = 'YouNet Media';
    
    const SUBJECT_COMMENT = '[YouNet Media] {PAGE} | {OBJECT}: new comment by {USER}';
    
	/*
	 * @Override
	 */
	public function __construct($charset = 'UTF-8')
	{
		$this->_mailer = new Zend_Mail($charset);
		$this->init();
	}
	
	protected function init()
	{
		$this->_mailer->setFrom($this->_from, $this->_from_name);
	}
	
	private function _getTemplateFile($template)
	{
		return YOUNET_PATH . '/Mail/views/' . $template;
	}
	
	public function sendEmail($to, $subject, $template, $data)
    {     
    	$view = new Zend_View();
    	foreach ($data as $name => $value)
    	{
    		$view->$name = $value;
    	}    	
    	$view->setScriptPath(YOUNET_PATH . '/Mail/views/');
    	$body = $view->render($template, $data);
    	
    	$this->_mailer->addTo($to);
        $this->_mailer->setSubject($subject);
        $this->_mailer->setBodyHtml($body);
        $this->_mailer->send();
    }
    
    public function sendCommentNotification($to, $data)
    {
    	$subject = str_replace('{PAGE}', $data['page_name'], self::SUBJECT_COMMENT);
    	$subject = str_replace('{OBJECT}', $data['object_name'], $subject);
    	$subject = str_replace('{USER}', $data['user_name'], $subject);
    	
    	$this->sendEmail($to, $subject, 'commentNotification.phtml', $data);
    }
    
    public function sendInvitationFriends($to, $message) 
    {
        $this->_mailer->clearFrom();
        $this->_mailer->setFrom('no-reply@younetco.com', $this->_from_name);
        $this->_mailer->addTo($to);
        $this->_mailer->setSubject($message['subject']);
        $this->_mailer->setBodyHtml($message['content']);
        $this->_mailer->send();
    }
}